package com.example.musicplayer

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicRecommendation:Fragment() {

    private val bannerImageURIList= mutableListOf<String>()  //存放banner图片URI路径的集合
    private val recDataList=ArrayList<rec>()  //存放rec数据的集合
    private lateinit var TitleImage:ImageView  //延迟初始化横幅图片
    private lateinit var RecommendedSong:RecyclerView  //延迟初始化滚动列表

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.musicpecommendation,container,false)
        TitleImage=view.findViewById(R.id.TitleImage)  //拿到Fragment实例中xml布局的图片对象
        RecommendedSong=view.findViewById(R.id.RecommendedSong)  //拿到MusicRecommendation这个Fragment实例中xml布局的滚动列表对象
        return view
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //音乐推荐导航背景图
        val JSONBannerService=ServiceCreator.create(JSONImageService::class.java)  //请求完整的网络连接，搭建接口的动态代理对象
        JSONBannerService.getBannerImage().enqueue(object :Callback<List<banner>> {  //让服务器返回的数据存入该方法创建的列表
            override fun onResponse(call: Call<List<banner>>, response: Response<List<banner>>) {
                val list=response.body()
                if (list!=null) {
                    for (banner in list) {
                        Log.d("MainActivity","bannerImageUrl is ${banner.bannerImgUrl}")  //检测是否得到图片的URL路径
                        bannerImageURIList.add(banner.bannerImgUrl)  //添加到集合中
                    }
                    //使用Glide添加通过Uri添加图片
                    Glide.with(this@MusicRecommendation)
                        .load(bannerImageURIList[2])
                        .into(TitleImage)
                }
            }
            override fun onFailure(p0: Call<List<banner>>, p1: Throwable) {
                Log.d("aaa","出错了吗?")
                p1.printStackTrace()
            }
        })
        //音乐推荐的滚动列表
        val JSONWithRecService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithRecService.getRecData().enqueue(object :Callback<List<rec>> {
            override fun onResponse(call: Call<List<rec>>, response: Response<List<rec>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        recDataList.add(i)  //添加网络请求返回的所有的rec类对象到集合中方便下面配置滚动菜单的适配器
                    }
                }
                val layoutManager=GridLayoutManager(context,3)
                RecommendedSong.layoutManager=layoutManager
                val adapter=RecommendedSongAdapter(recDataList)
                RecommendedSong.adapter=adapter
            }
            override fun onFailure(p0: Call<List<rec>>, p1: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }


}